import express from 'express';
import db from './../../lib/dbHelper';
let adminRouter=express.Router();

//index页面
adminRouter.get("/index",(request,response,next)=>{
    let muenSQL="SELECT*FROM `sys_menu` WHERE m_status=1 ORDER BY m_orderby ";
    let webSQL = "SELECT*FROM `websteinfo` ";
    db.query(webSQL,[],function(err,res1){
	    db.query(muenSQL,[],function(err,res2){ 
		    response.render("./admin/index.html",{menu:res2,webinfo:res1});		 
	    });
    })
	//response.end("admin/index");
})

//meun 页面
adminRouter.get("/meun",(request,response,next)=>{
     response.render("./admin/meun.html",{});
})

//获取menu页面的数据
adminRouter.post("/getmeun",(request,response, next)=>{
    //查询数据库 
    let queryMeunSql="SELECT*FROM `sys_menu` WHERE m_status=1 ORDER BY m_orderby ";
    db.query(queryMeunSql,[],function(err,result){
        let obj={
            "code":0,
            "msg":"",
            "count":result.length,
            "data":result
        }
        if(!err){
            response.json(obj);
        }
    })    
});


//刪除的接口
adminRouter.post("/deleteByMeunId",(request,response,next)=>{

 //软刪除的sql语句 (修改状态)
    let queryMeunDeleteSql="UPDATE sys_menu SET m_status=0 WHERE m_id=?";
    db.query(queryMeunDeleteSql,[request.body.mId],function(err,result){
        //affestedRows 受影响的行数
        if(!err&&result.affectedRows>=1){
            response.json({
                msg:"删除成功",
                "status":1
            });
        }else{
            response.json({
                msg:"删除失败",
                "status":-1,
                err:err
            });
        }
    })    
})

//通过id修改内容
adminRouter.post("/updateMeun",(request,response,next)=>{
    //修改的sql语句
    console.log(request.body)
    let updateMeunSql="UPDATE `sys_menu` SET m_name=?,m_url=?,m_no=?,m_parents=?,m_icon=?,m_orderby=?,m_status=? WHERE m_id=?";
    //let updateMeunSql="UPDATE `sys_menu` SET m_name=?,m_url=?,m_no=?,m_parents=?,m_icon=?,m_orderby=? WHERE m_id=?";
   
    let params=[
        request.body.m_name,
        request.body.m_url,
        request.body.m_no,
        request.body.m_parents,
        request.body.m_icon,
        request.body.m_orderby,
        request.body.m_status,
        request.body.m_id
    ];
    db.query(updateMeunSql,params,function(err,result){
        //affestedRows 受影响的行数
        if(!err&&result.affectedRows>=1){
            response.json({
                msg:"修改成功",
                "status":1
            });
        }else{
            response.json({
                msg:"修改失败",
                "status":-1,
                err:err
            });
        }
    })    
})






module.exports=adminRouter;